﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm.Locate;
using NetTopologySuite.Geometries.Utilities;

namespace NetTopologySuite.Operation.Predicate
{
	internal class GeometryContainsPointVisitor : ShortCircuitedGeometryVisitor
	{
		public GeometryContainsPointVisitor(IPolygon rectangle)
		{
			this._rectSeq = rectangle.ExteriorRing.CoordinateSequence;
			this._rectEnv = rectangle.EnvelopeInternal;
		}

		public bool ContainsPoint { get; private set; }

		protected override void Visit(IGeometry geom)
		{
			if (!(geom is IPolygon))
			{
				return;
			}
			Envelope envelopeInternal = geom.EnvelopeInternal;
			if (!this._rectEnv.Intersects(envelopeInternal))
			{
				return;
			}
			Coordinate coordinate = new Coordinate();
			for (int i = 0; i < 4; i++)
			{
				this._rectSeq.GetCoordinate(i, coordinate);
				if (envelopeInternal.Contains(coordinate) && SimplePointInAreaLocator.ContainsPointInPolygon(coordinate, (IPolygon)geom))
				{
					this.ContainsPoint = true;
					return;
				}
			}
		}

		protected override bool IsDone()
		{
			return this.ContainsPoint;
		}

		private readonly ICoordinateSequence _rectSeq;

		private readonly Envelope _rectEnv;
	}
}
